package com.ego.item.web;

import com.ego.common.pojo.PageResult;
import com.ego.item.pojo.Brand;
import com.ego.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    //pageNo=1&pageSize=5&sortBy=id&descending=false&key=o
    public ResponseEntity<PageResult<Brand>> page(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("sortBy") String sortBy,
            @RequestParam("descending") Boolean descending,
            @RequestParam("key") String key){
        PageResult<Brand> result=brandService.page(pageNo,pageSize,sortBy,descending,key);
        if(result==null||result.getItems().size()==0){
          return   ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
    @PostMapping
    public ResponseEntity<Void> save(Brand brand ,@RequestParam("cids") List<Integer> cids){
        brandService.save(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @GetMapping("/bid/{bid}")
    public ResponseEntity<Brand> queryBrand(@PathVariable("bid")Long bid){
       Brand result= brandService.findBybid(bid);
       return ResponseEntity.ok(result);
    }
    @PutMapping
    public ResponseEntity<Void> update(Brand brand ,@RequestParam("cids")List<Integer> cids){
        brandService.update(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @DeleteMapping("/{bid}")
    public ResponseEntity<Void> deleteById(@PathVariable("bid")Long bid){
        brandService.deleteById(bid);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //  /cid/3
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid")Long cid){
        List<Brand> result= brandService.findByCid(cid);
        if(result==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
}
